// pages/chooseStaff/chooseStaff.js
const app = getApp()
const api = app.globalData.api
Page({

  /**
   * 页面的初始数据
   */
  data: {
    people:0,
    flowName:'',//流程名称
    id:'',//待审核数据的ID
    type:'',//转交/增加审批人
    title:'选择审批人',
    commit_name:'',
    ico:'',
    commit_name_id:'',
    isBack:false,
    staff:[]
  },

  /**删除 */
  deleteStaff(e){
    let id = e.currentTarget.dataset.id;
    let arr = this.data.staff.filter(item => item.stid !=id)
    this.setData({
      people:this.data.people-1,
      staff:arr
    })
  },
  /**选择员工 */
  addStaff(){
    wx.navigateTo({
      url: '../staff/staff',
    })
  },
  /**确定选择 */
  confirm(){
    let that = this;
    let id = this.data.id;
    let para = {
      id:id,
      flowName:this.data.flowName,
      act:'other'
    }
    let act = '';
    if(this.data.type =='transfer'){
      act = 'transfer';
    }else if(this.data.type =='add'){
      act = 'addApprover';
    }
    if(this.data.type=='addAuditor'){
      para.act = 'start';
      api.curlPost('?cla=auditApp&fun=getPara',para).then((res)=>{
        let staff = JSON.stringify(that.data.staff);
        api.curlPost('?cla=auditApp&fun=addAuditPeopleEdit',{id:id,body:res.data.result,stid:staff}).then((back)=>{
            wx.navigateBack({
              delta: 1,
            })
        })
      })
    }else if(this.data.type=='client'){
      //客户转给他人
      
      let staff = this.data.staff[0].stid;
      api.curlPost('?cla=clientAppClass&fun=transferEdit',{manager:staff,khid:id}).then((back)=>{
          wx.navigateBack({
            delta: 2,
          })
      })
    }else if(this.data.type == 'distribute'){
      var pages = getCurrentPages();
      var prevPage = pages[pages.length - 2]; //上一个页面
      let client_list = prevPage.data.list;
  
      let arr = [];
      client_list.forEach((v)=>{
        if (v.checked) {
          arr.push(v.khid)
        }
      })
      let managerId = this.data.staff[0].stid;
      let khid = arr.join(',');
      api.curlPost('?cla=clientOtherApp&fun=transferEdit',{khid:khid,manager:managerId}).then((back)=>{
        wx.navigateBack({
          delta: 1,
        })
    })
      
    }else{
      api.curlPost('?cla=auditApp&fun=getPara',para).then((res)=>{
        let staff = JSON.stringify(that.data.staff);
        api.curlPost('?cla=auditApp&fun=operate',{id:id,act:act,body:res.data.result,stid:staff}).then((back)=>{
            wx.navigateBack({
              delta: 1,
            })
        })
      })
    }
    

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      type:options.type,
      flowName:options.flowName,
      id:options.id,
      title:(options.type=='client'|| options.type=='distribute')?'选择负责人':this.data.title
    })
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; //上一个页面
    //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
    prevPage.setData({
      isBack:true,
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(this.data.isBack){
      let staff = this.data.staff;
      let data = {
        stid:this.data.commit_name_id,
        name:this.data.commit_name,
        ico:this.data.ico,
      }
      //去重
      let isRepeat = false;
      staff.forEach((v)=>{
        if(v.stid==data.stid){
          isRepeat = true;
        }
      })
      let people = this.data.people;
      if(!isRepeat){
        staff.push(data);
        people++;
      }
      this.setData({
        staff:staff,
        people:people,
        isBack:false,
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})